package com.graducationdesign.springboot.controller.Admin;

import com.graducationdesign.springboot.entities.Administrator.PersonnalInformationMoudle.Student;
import com.graducationdesign.springboot.mapper.StudentMapper;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author Mr.zhneg
 * @create 2019-11-02-21:32
 */
//学生信息管理控制层
@Controller
public class StudentInformationController {

    org.slf4j.Logger logger=  LoggerFactory.getLogger(getClass());

    @Autowired
    StudentMapper studentMapper;

//    @RequestMapping(value = "personnaleinformation",method = RequestMethod.GET)
//    等价于下面注解
    //查看学生信息管理的主界面，并且可以查询学生名字

    @GetMapping("/student")
    public String list(Map<String,Object> map,Student student){ //@RequestParam(value = "studentName",required = false) String studentName,

        logger.info("学生信息查询。。。"+student);
//        Collection<Student> students=studentDao.getAll(studentName);

        List<Student> students=studentMapper.getStudents(student);
        logger.info("查询操作：学生信息查询结果。。。"+students);
        map.put("students",students);
        //查询学生的时候可以让数据回显
//        map.put("studentName",studentName);
        map.put("studentName",student.getName());

//        String DEFAULT_PREFIX = "classpath:/templates/";    
// String DEFAULT_SUFFIX = ".html";
//        模板文件放在上述位置

        return "Administrator/personnalInformationModule/studentinformationmanagement";
//        return "Administrator/personnalInformationModule/studentinformationmanagementview";

    }

    //查看学生信息，和前往学生修改页面
    @GetMapping("/student/{id}")
    public String view(@PathVariable("id") Integer id,@RequestParam(value = "type",defaultValue = "studentinformationmanagementview") String type,Map<String,Object> map){

        logger.info("查看操作：查询学生编码为"+id+"的学生详细信息");

//       Student student= studentDao.getStudnet(id);
        Student student=studentMapper.getStudentByPid(id);
       map.put("student",student);

       //type=null 则进入studentinformationmanagementview.html ,type=update则进入studentinformationmanagementupdate.html
        return "Administrator/personnalInformationModule/"+type;
//       return "Administrator/personnalInformationModule/studentinformationmanagementview";


    }

    //修改学生信息
    @PutMapping("/students")
//    保证studentinformationmanagementupdate.html中的name值要和Student类中的属性相对应
    public String update(Student student){

        logger.info("修改操作：修改学生的信息"+student);
        studentMapper.updateStudent(student);

        return "redirect:student";
    }

//    进入添加学生页面
    @GetMapping ("/students")
    public String toAddPage(){

        return "Administrator/personnalInformationModule/studentinformationmanagementadd";
}

    //    在添加学生页面中添加学生信息
    @PostMapping("/students")
    public String add(Student student){

        logger.info("添加操作：添加学生信息。。"+student);
        //保存数据操作
       studentMapper.addStudent(student);
        return "redirect:/student";
    }

    //删除学生信息
    @DeleteMapping("/studnets/{id}")
    public String delete(@PathVariable("id") Integer id){

        logger.info("删除操作：删除编号为"+id+"的学生");
        studentMapper.deleteStudentById(id);
        return "Administrator/personnalInformationModule/studentinformationmanagement";

    }
}
